﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using Microsoft.Practices.Unity;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.ContentManagementFramework.Web.StartUp;
using Fuse8.ContentManagementFramework.Web.ApplicationRuntime;
using Fuse8.ContentManagementFramework.Web.Etc;

namespace Fuse8.CMF.BaseWebsite
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        private IUnityContainer _container;
        private UnityServiceLocatorWithLazy _locator;

        protected void Application_Start()
        {
            _container = new UnityContainer();

            _locator = new UnityServiceLocatorWithLazy(_container);

            DependencyServiceLocator.SetLocatorProvider(() => _locator);

            CmsPolicy.Init(
                _container,
                new CmsPolicyOptions
                {
                    Filters = GlobalFilters.Filters,
                    Routes = RouteTable.Routes
                });
            
            ViewEngines.Engines.AddCustom<RazorViewEngine>(p => p.IsInArea("/admin-custom"), "admin-custom");
            ViewEngines.Engines.Add(new RazorViewEngine());
        }

        public MvcApplication()
        {
            PostReleaseRequestState += (o, eventArgs) =>
            {
                if (!Context.IsDebuggingEnabled && (Response.ContentType == "text/html"))
                {
                    Response.Filter = new WhiteSpaceFilter(Response.Filter);
                }
            };
        }
    }
}